/*
* $Id: ChangeFamilyView.cpp 154 2010-09-27 08:10:48Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ChangeFamilyView.h"
#include "ViewFieldValuesHelper.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Visual/Drawers/EditorDrawer/ComboBoxItem.h"
#include "Settings/Journal/FamilyViewSettings.h"
#include "Settings/SettingControlIds.h"
#include "Settings/DinIntDefined.h"
#include "Components/Journal/FamilyManager.h"

#pragma warning(disable: 4407)

#define TITLE_ID			1
#define NAME_FAMILY_ID		2
#define FAMILY_TYPE_ID		3
#define NOTE_FAMILY_ID		4
#define USER_FIELD_ID		5

CChangeFamilyView::CChangeFamilyView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){}
CChangeFamilyView::~CChangeFamilyView(){}

void CChangeFamilyView::Init(){
	m_editorDrawer.Clear();
	m_editorDrawer.AddItem(new CHeaderItem(ILanguageManager::GetLanguageManager()->GetString(CHANGE_FAMILY_ID), TITLE_ID));

	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	CFamilyManager::family fml = pManager->GetFamily(m_nIdParameter);
	m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_NAME_ID), NAME_FAMILY_ID, editItem::TEXT, fml.m_strName));
	CFamilyViewSettings familyStn;
	CComboBoxItem* pItem = new CComboBoxItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_FAMILY_TYPE_ID), FAMILY_TYPE_ID);
	AddTypesFamily(pItem);
	pItem->SetValue(fml.m_type);
	m_editorDrawer.AddItem(pItem);
	if(familyStn.GetCurrentValue(CHECK_TYPE_ID) != CHECK_BOX_STATE_1)
		pItem->Hide(true);
	CEditItem *pEditItem = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_FAMILY_ID, editItem::TEXT, fml.m_strNote);
	m_editorDrawer.AddItem(pEditItem);
	if(familyStn.GetCurrentValue(CHECK_NOTE_ID) != CHECK_BOX_STATE_1)
		pEditItem->Hide(true);
	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(TypeField::FAMILIES, USER_FIELD_ID, fml.m_nIdRecord);
}

void CChangeFamilyView::SetState(){
	m_pState->m_nState = Journal::VIEW_CHANGE_FAMILY;
}
bool CChangeFamilyView::IsViewState(State state){
	return state.m_type == Journal::CHANGE_FAMILY;
}

BEGIN_MESSAGE_MAP(CChangeFamilyView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSaveFamily)
END_MESSAGE_MAP()

void CChangeFamilyView::OnSaveFamily(){
	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	CFamilyManager::family fml;
	fml.m_strName = m_editorDrawer.GetValueItem(NAME_FAMILY_ID);
	fml.m_strNote = m_editorDrawer.GetValueItem(NOTE_FAMILY_ID);
	fml.m_type = (Family::type)_ttoi(m_editorDrawer.GetValueItem(FAMILY_TYPE_ID).c_str());
	std::map<std::tstring, std::tstring> aUserFields;
	for(int i = 1; i <= m_nUserFields; i++)
		aUserFields.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
	pManager->ChangeFamily(m_nIdParameter, fml, aUserFields);
	__super::OnCancel();
}

void CChangeFamilyView::AddTypesFamily(CComboBoxItem* pItem){
	pItem->AddValueItem(Family::DEFAULT, ILanguageManager::GetLanguageManager()->GetString(TYPE_DEFAULT_ID));
	pItem->AddValueItem(Family::PEDIGREE, ILanguageManager::GetLanguageManager()->GetString(TYPE_PEDIGREE_ID));
	pItem->AddValueItem(Family::BREEDING, ILanguageManager::GetLanguageManager()->GetString(TYPE_BREEDING_ID));
	pItem->AddValueItem(Family::FATHER, ILanguageManager::GetLanguageManager()->GetString(TYPE_FATHER_ID));
	pItem->AddValueItem(Family::AMADOU, ILanguageManager::GetLanguageManager()->GetString(TYPE_AMADOU_ID));
	pItem->AddValueItem(Family::BARREN, ILanguageManager::GetLanguageManager()->GetString(TYPE_BARREN_ID));
	pItem->AddValueItem(Family::QUEEN_CELL, ILanguageManager::GetLanguageManager()->GetString(TYPE_QUEEN_CELL_ID));
	pItem->AddValueItem(Family::QUEEN_CELL_SMALL, ILanguageManager::GetLanguageManager()->GetString(TYPE_QUEEN_CELL_SMALL_ID));
	pItem->AddValueItem(Family::INDIVIDUAL_PROVINE, ILanguageManager::GetLanguageManager()->GetString(TYPE_INDIVIDUAL_PROVINE_ID));
	pItem->AddValueItem(Family::COMBINED_PROVINE, ILanguageManager::GetLanguageManager()->GetString(TYPE_COMBINED_PROVINE_ID));
	pItem->AddValueItem(Family::PACKET, ILanguageManager::GetLanguageManager()->GetString(TYPE_PACKET_ID));
	pItem->AddValueItem(Family::NON_QUEEN, ILanguageManager::GetLanguageManager()->GetString(TYPE_NON_QUEEN_ID));
	pItem->AddValueItem(Family::SWARM, ILanguageManager::GetLanguageManager()->GetString(TYPE_SWARM_ID));
	pItem->AddValueItem(Family::USER, ILanguageManager::GetLanguageManager()->GetString(TYPE_USER_ID));
}
